#!/usr/bin/perl
# ES/Nif.pm
use strict;

package ES::Nif;
# Check that the letter of the Spanish NIF is correct. A Spanish personal
# NIF is a set of numbers followed by a letter. It can be expressed in upper
# or lower case, separated by a dash or not.
 

use Carp qw(carp);

sub check {
   my $nif = shift;
   carp "Incorrect NIF format: $nif \n" if $nif !~ /^(\d+)\-?(\w)$/;
   my ($number, $letter) = ($1, $2);
   return letter($number) eq uc($letter);
}

sub letter {
   my $number = shift;
   my $mod = $number % 23;
   my ($digit1, $digit2) = split(//, $mod);
   my $letterComputed = $::lookup[$digit1][$digit2];
   return $letterComputed;
}
INIT {
   my $i = 0;
   while (<DATA>) {
      my @data = split;
      push(@{$::lookup[$i++]}, @data);
   }
}
"No te lo digo";   # this is a true value
# taken from http://www.terra.es/personal2/bomb009/telefonos.htm

__DATA__
T R W A G M Y F P D
X B N J Z S Q V H L
C K E T
